<?php
namespace app\api\controller;

use app\api\model\CourseArticle;
use app\api\model\UserSubscribe;


class Course extends Basic
{
    private $Course;
    private $Praise;
    private $TalkShow;
    private $CourseArticle;
    private $UserSubscribe;
    public $types = [
        1=>'免费订阅',
        2=>'非会员试听',
        3=>'非会员试听',
        4=>'非会员试读',
    ];
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->Course=new \app\api\model\Course();
        $this->CourseArticle=new CourseArticle();
        $this->TalkShow=new \app\api\model\TalkShow();
        $this->Praise=new \app\api\model\Praise();
        $this->UserSubscribe=new UserSubscribe();
    }

    /**
     * 小课首页  小课列表
     */
    public function course_list()
    {
        $page=input('page',1);
        $limit=input('limit',20);
        $status['status']=['eq',1];
        $type['type']=['eq',3];//订阅类型为3 小课

        $data['list']=$this->Course->field('id,title,hits,type,ctype,price,sub_title,picture,cover')->where($status)->where(['is_video'=>0])->order('sort desc,create_time desc')->page($page,$limit)->select();
        $data['count']=$this->Course->where(['is_video'=>0])->where($status)->count();
        //获取用户是否订阅了小课
        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['is_video'] = 0;
            //查找订阅类型为有效的小课记录  status=1
            $res=$this->UserSubscribe->where($type)->where('user_id',UID)->where('status',1)->where('pid',$v['id'])->find();
          $article=$this->CourseArticle->field('title')->where('course_id',$v['id'])->where('status',1)->order('sort')->find();
            $data['list'][$k]['article_title']=$article['title'];
            if (!empty($res)){
                //已订阅
                $data['list'][$k]['subscribe_status']=1;
            }else{
                //已过期或未订阅
                $data['list'][$k]['subscribe_status']=2;
            }
            // 点赞数量
            // 所有的小课
            $data['list'][$k]['praise_num'] = $data['list'][$k]['hits'] = 0;
            $articles=$this->CourseArticle->where('course_id',$v['id'])->where('status',1)->order('sort')->select();
            if($articles) {
                foreach ($articles as $a) {
                    if($a['video_url'])
                        $data['list'][$k]['is_video'] = 1;
                    $data['list'][$k]['praise_num'] = $data['list'][$k]['praise_num']+$this->Praise->where('type',1)->where('oid',$a['id'])->count()+$a['base_praise'];
                    $data['list'][$k]['hits'] = $data['list'][$k]['hits']+$a['hits']+$a['base_hits'];
                }
            }
            $data['list'][$k]['praise_num'] = $this->formatNum($data['list'][$k]['praise_num']);
            $data['list'][$k]['hits'] = $this->formatNum($data['list'][$k]['hits']);
            $data['list'][$k]['type'] = isset($this->types[$data['list'][$k]['type']])?$this->types[$data['list'][$k]['type']]:'';

        }
        if (!empty($data)){
            json_success($data);
        }
        json_success([]);

    }

    /**
     * 通过小课ID 获取小课介绍与小课目录
     */
    public function course()
    {
        //获取小课ID
        $id=input('id','');
        if (empty($id)){
            json_fail('缺少ID参数');
        }
        //获取小课的介绍与内容
      $data['read']=$this->Course->field('id,title,sub_title,hits,is_video,cover,picture,type,price,content')->where('id',$id)->find();
      $data['read']['price']=$data['read']['price']/100;
        //获取小课目录列表
      $praise_num_all = $hits_all = 0;
      $data['course_article_list']=$this->CourseArticle->field('id,title,hits,base_hits,base_praise,duration,picture')->where('course_id',$id)->where('status',1)->order('sort asc')->select();
        foreach ($data['course_article_list'] as $k=>$v) {
            if ($v['duration'] >= 3600) {
                //求小时数
                $hour=(int)($v['duration']/3600);
                $s=$v['duration']%3600;
                $minute=(int)($s/60);
                $s=$v['duration']%60;
                $s=$s%60;


                $data['course_article_list'][$k]['duration']=$hour.':'.$minute.':'.$s;
            } else {
                $data['course_article_list'][$k]['duration'] = date('i:s', $v['duration']);
            }
            $praise_num = $this->Praise->where('type',1)->where('oid',$v['id'])->count();
            $praise_num_all = $praise_num_all+$praise_num+$v['base_praise'];
            $hits_all = $hits_all+$v['hits']+$v['base_hits'];
            $data['course_article_list'][$k]['praise_num'] = $this->formatNum($praise_num+$v['base_praise']);
            $data['course_article_list'][$k]['hits'] = $this->formatNum($v['hits']+$v['base_hits']);
        }
        $data['read']['praise_num'] = $this->formatNum($praise_num_all);
        $data['read']['hits'] = $this->formatNum($hits_all);
      //判断小课是否被订阅 获取有效的小课订阅记录
        $res=$this->is_subscribe($id,3);
        if (empty($res)){
            $data['subscribe_status']=2;
        }else{
            $data['subscribe_status']=1;
        }

        //获取用户是否是VIP  1是VIP  2 不是VIP
        $vip=$this->is_vip();
        $data['vip']=$vip;
        $data['type']=3;
        if (!empty($data)){
            json_success($data);
        }
        json_success([]);

    }

    /**
     * 点赞接口
     */
    public function add_praise()
    {
        $id=input('id');
        $type=input('type');
        if (empty($id)||empty($type)){
            json_fail('缺少必要参数');
        }
        if($type==1) {
            $obj = $this->CourseArticle;
        } else {
            $obj = $this->TalkShow;
        }
        if($this->Praise->where('type',$type)->where('uid',UID)->where('oid',$id)->find()) {
            json_fail('您已点赞，请勿重复点赞');
        }
        // 新建点赞
        $this->Praise->data(['uid' => UID, 'oid' => $id,'type'=>$type,'create_time'=>time()])->save();
        json_success([]);
    }


    /**
     * 小课详情 音频文章
     */
    public function course_read()
    {
        //获取小课文章ID
        $id=input('id','');
        if (empty($id)){
            json_fail('缺少ID参数');
        }
        $type['type']=['eq',3];
        //小课文章所有 内容
        $data=$this->CourseArticle->where('id',$id)->where('status',1)->find();

        if (empty($data)){
            json_fail('无小课');
        } else {
            // 播放数+1
            $hits = $data['hits'] + 1;
            $this->CourseArticle->where('id',$id)->update(['hits'=>$hits]);
            // 他的小课
            $parentCourse = $this->Course->where('id',$data['course_id'])->find();
            if($parentCourse) {
                $data['type'] = $parentCourse['type'];
                $data['parent_price'] = $parentCourse['price']/100;;
                $parentCourseHits = $parentCourse['hits'] + 1;
                $this->Course->where('id',$data['course_id'])->update(['hits'=>$parentCourseHits]);
                $res=$this->UserSubscribe->where($type)->where('user_id',UID)->where('status',1)->where('pid',$data['course_id'])->find();
                if (!empty($res)){
                    //已订阅
                    $data['subscribe_status']=1;
                }else{
                    //已过期或未订阅
                    $data['subscribe_status']=2;
                }
            }
        }

//        if ($data['duration']>=3600){
//            //求小时数
//            $hour=(int)($data['duration']/3600);
//            $s=$data['duration']%3600;
//            $minute=(int)($s/60);
//            $s=$data['duration']%60;
//            $s=$s%60;
//            $data['duration']=$hour.':'.$minute.':'.$s;
//        }else{
//            $data['duration']=date('i:s',$data['duration']);
//        }

        //上一条
        $prev_id=$this->CourseArticle->field('id')->where('course_id',$data['course_id'])->where('sort','<',$data['sort'])->where('status',1)->order('sort desc')->value('id');

        //下一条
        $next_id=$this->CourseArticle->field('id')->where('course_id',$data['course_id'])->where('sort','>',$data['sort'])->where('status',1)->order('sort asc')->value('id');
        // 是否点赞
        $data['is_praise']=$this->Praise->where('type',1)->where('uid',UID)->where('oid',$id)->count();
        $data['praise_num']=$this->formatNum($this->Praise->where('type',1)->where('oid',$id)->count()+$data['base_praise']);
        $data['hits']=$this->formatNum($data['hits']+$data['base_hits']);
        $data['prev_id']=$prev_id;
        $data['next_id']=$next_id;
        $data['can_video']=1;
        $vip=$this->is_vip();
        $data['vip']=$vip;
        
        // $data['price']=$next_id;
        // $data['short_content']=$next_id;
        if (!empty($data)){
            json_success($data);
        }
        json_success([]);
    }


    /**
     * 小课文章页面底部目录
     */

        public function course_article_list()
        {
            //获取小课文章中的小课ID
            $course_id=input('course_id','');
            if (empty($course_id)){
                json_fail('缺少ID参数');
            }
            $data=$this->CourseArticle->field('id,title,hits,base_hits,video_url,base_praise,duration')->where('course_id',$course_id)->where('status',1)->order('sort asc')->select();
               foreach ($data as $k=>$v){
                   if ($v['duration']>=3600){
                       //求小时数
                       $hour=(int)($v['duration']/3600);
                       $s=$v['duration']%3600;
                       $minute=(int)($s/60);
                       $s=$v['duration']%60;
                       $s=$s%60;
                       $data[$k]['duration']=$hour.':'.$minute.':'.$s;
                   }else{
                       $data[$k]['duration']=date('i:s',$v['duration']);

                   }
                   // 点赞数量
                   $data[$k]['praise_num'] = $this->formatNum($this->Praise->where('type',1)->where('oid',$v['id'])->count()+$v['base_praise']);
                   $data[$k]['hits'] = $this->formatNum($v['hits']+$v['base_hits']);
               }
            if (!empty($data)){
                json_success($data);
            }
            json_success([]);
        }

}